/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kallsonys.common;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

/**
 *
 * @author allan
 */
@ManagedBean(name="commonBean")
public class commonBean {
    
     @EJB
    private com.kallsonys.session.OrderOmsFacade ejbFacade;

    /** Creates a new instance of commonBean */
    public commonBean() {
    }
    
    /*
     * Obtengo el Contexto
     */
    public ExternalContext getContext() {
        ejbFacade.getEntityManager().getEntityManagerFactory().getCache().evictAll();
        
        return FacesContext.getCurrentInstance().getExternalContext();
    }
    
    /**
     * Retorna los Roles a los que Pertenece el Usuario
     * @return 
     */
    public List<String> getUserRoles()
    {
        List<String> lstRetorno = new ArrayList<String>();
        
        String[] lstRoles = getLocalRoles();
        
        for ( String rol : lstRoles )
        {
            if ( getContext().isUserInRole(rol) )
            {
                lstRetorno.add(rol);
            }
        }
        return lstRetorno;
    }
    
    public boolean isProductosConsulta() { return getContext().isUserInRole("ProductosConsulta") || getContext().isUserInRole("Administrador") ; }
    public boolean isProductosAdmon() { return getContext().isUserInRole("ProductosAdmon") || getContext().isUserInRole("Administrador") ;  }
    public boolean isProductosAdmonConsulta() { return getContext().isUserInRole("ProductosAdmon") || getContext().isUserInRole("ProductosConsulta") || getContext().isUserInRole("Administrador") ;  }
    public boolean isCampannas() { return getContext().isUserInRole("Campannas") || getContext().isUserInRole("Administrador") ; }
    public boolean isOrdenesConsulta() { return getContext().isUserInRole("OrdenesConsulta") || getContext().isUserInRole("Administrador") ; }
    public boolean isOrdenesAdmon() { return getContext().isUserInRole("OrdenesAdmon") || getContext().isUserInRole("Administrador") ; }
    public boolean isOrdenesAdmonConsulta() { return getContext().isUserInRole("OrdenesAdmon") || getContext().isUserInRole("OrdenesConsulta") || getContext().isUserInRole("Administrador") ; }
    public boolean isClientesConsulta() { return getContext().isUserInRole("ClientesConsulta") || getContext().isUserInRole("Administrador") ; }
    public boolean isClientesAdmon() { return getContext().isUserInRole("ClientesAdmon") || getContext().isUserInRole("Administrador") ; }
    public boolean isClientesAdmonConsulta() { return getContext().isUserInRole("ClientesConsulta") || getContext().isUserInRole("ClientesAdmon") || getContext().isUserInRole("Administrador") ; }
    public boolean isAdministrador() { return getContext().isUserInRole("Administrador") || getContext().isUserInRole("Administrador") ; }
    
    /**
     * Roles Locales
     * @return 
     */
    private String[] getLocalRoles()
    {
        return new String[] {"ProductosConsulta",
                             "ProductosAdmon",
                             "Campannas",
                             "OrdenesConsulta",
                             "OrdenesAdmon",
                             "ClientesConsulta",
                             "ClientesAdmon",
                             "Administrador"};
    }
}
